﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TerminationCall.Core;

namespace TerminationCall.Editor.GUI {
    public partial class TileSetPropertiesForm : Form {

        private TileSet _tileSet;

        public TileSetPropertiesForm(TileSet tileSet) {
            InitializeComponent();

            _tileSet = tileSet;

            edtName.Text = _tileSet.Name;
            pibImage.Image = _tileSet.Bitmap;

            tileSetRenderAutoborder.TileSet = tileSet;
            tileSetRenderObjects.TileSet = tileSet;
        }

        private void btoCancel_Click(object sender, EventArgs e) {

            DialogResult = DialogResult.Cancel;

            this.Close();
            
        }

        private void btoOk_Click(object sender, EventArgs e) {

            _tileSet.Name = edtName.Text;
            _tileSet.Bitmap = pibImage.Image as Bitmap;

            DialogResult = DialogResult.OK;

            this.Close();

        }

        private void btoChangeImage_Click(object sender, EventArgs e) {

            if (ofdChangeImage.ShowDialog() == DialogResult.Cancel) {
                return;
            }

            pibImage.Image = new Bitmap(Image.FromFile(ofdChangeImage.FileName));

        }

        private void btoGroupSelectionToAutoborder_Click(object sender, EventArgs e) {
                        
            int y = tileSetRenderAutoborder.Selection.Y + 1;
            int height = tileSetRenderAutoborder.Selection.Height;

            while (height >= 3) {

                int x = tileSetRenderAutoborder.Selection.X + 1;
                int width = tileSetRenderAutoborder.Selection.Width;

                while (width >= 3) {
                    
                    GroupToAutoborder(x, y);

                    width = width - 3;
                    x = x + 3;

                }

                height = height - 3;
                y = y + 3;

            }

            tileSetRenderAutoborder.Refresh();

        }

        private void GroupToAutoborder(int x, int y) {

            if (_tileSet.Tiles.GetLength(0) < x + 1) {
                return;
            }

            if (_tileSet.Tiles.GetLength(1) < y + 1) {
                return;
            }

            Tile middleTile = _tileSet.Tiles[x, y];

            _tileSet.GroupAutoborderTile(TileFields.TopTile, _tileSet.Tiles[x, y - 1],middleTile);
            _tileSet.GroupAutoborderTile(TileFields.BottomTile, _tileSet.Tiles[x, y + 1], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.LeftMiddleTile, _tileSet.Tiles[x - 1, y], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.RightMiddleTile, _tileSet.Tiles[x + 1, y], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.LeftTopTile, _tileSet.Tiles[x - 1, y - 1], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.RightTopTile, _tileSet.Tiles[x + 1, y - 1], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.LeftBottomTile, _tileSet.Tiles[x - 1, y + 1], middleTile);
            _tileSet.GroupAutoborderTile(TileFields.RightBottomTile, _tileSet.Tiles[x + 1, y + 1], middleTile);
            
        }

        private void btoUngroupSelection_Click(object sender, EventArgs e) {

            for (int x = tileSetRenderAutoborder.Selection.Left; x < tileSetRenderAutoborder.Selection.Right; x++) {
                for (int y = tileSetRenderAutoborder.Selection.Top; y < tileSetRenderAutoborder.Selection.Bottom; y++) {
                    _tileSet.UngroupAutoborderTile(_tileSet.Tiles[x, y]);                    
                }
            }

            tileSetRenderAutoborder.TileSet = _tileSet;

        }

        private void btoGroupSelectionToObject_Click(object sender, EventArgs e) {
            _tileSet.GroupObjectTile(tileSetRenderObjects.Selection, "Object" + (_tileSet.TileObjectGroups.Count + 1));

            tileSetRenderObjects.TileSet = _tileSet;
        }

        private void btoUngroupSelectionFromObject_Click(object sender, EventArgs e) {
            _tileSet.UngroupObjectTile(tileSetRenderObjects.Selection.X, tileSetRenderObjects.Selection.Y);

            tileSetRenderObjects.TileSet = _tileSet;
        }
        
    }
}
